extends Area2D

# 碰撞信号
signal hit 

# 速度
export var speed = 400
var screen_size

var score


# Called when the node enters the scene tree for the first time.
func _ready():
	# 获取屏幕尺寸
	screen_size = get_viewport_rect().size


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	# 获取速度
	var velocity = Vector2()
	
	# 根据按键设置速度
	if Input.is_action_pressed("ui_right"):
		velocity.x += 0.5
	if Input.is_action_pressed("ui_left"):
		velocity.x -= 0.5
	if Input.is_action_pressed("ui_down"):
		velocity.y += 0.5
	if Input.is_action_pressed("ui_up"):
		velocity.y -= 0.5
	
	print_debug(velocity.length())
	
	
	# 速度归一化
	if velocity.length() > 0:
		velocity = velocity.normalized() * speed
		$AnimatedSprite.play()
	else:
		$AnimatedSprite.stop()
		
	# 移动 （并且防止溢出屏幕）
	position += velocity * delta
	position.x = clamp(position.x, 0, screen_size.x)
	position.y = clamp(position.y, 0, screen_size.y)
	
	# 切换动画
	if velocity.x != 0:
		# 切换动画至walk
		$AnimatedSprite.animation = "walk"
		# 禁止反转纵向
		$AnimatedSprite.flip_v = false
		# 根据速度进行反转
		$AnimatedSprite.flip_h = velocity.x < 0
	elif velocity.y != 0:
		# 切换动画至up
		$AnimatedSprite.animation = "up"
		# 根据速度进行反转
		$AnimatedSprite.flip_v = velocity.y > 0
	
	

# 碰撞信号
func _on_Player_body_entered(body):
	# 隐藏当前节点
	hide()
	
func start(pos):
	position = pos
	show()
	$CollisionShape2D.disabled = false





